﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biblioteca.DataBase;
using Pms.Utils;
using System.Data;
using SEG.Utils;
using Biblioteca.Utils;
using System.Data.Common;
using Biblioteca.Objetos;
using SEG.Objetos;

namespace SEG
{
    public partial class InterfacesManut : System.Web.UI.Page
    {

        Usuario usuAut;
        char permManut;
        string cod_int;
        string cod_aplic;
        string operacao;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.Count == 0)
                Response.Redirect("~/Login.aspx");
            else
            {
                lblErro.Visible = false;

                usuAut = (Usuario)Session["usuario"];

                if (Request["OP"] != null)
                {
                    operacao = Request["OP"];
                }

                if (Request["codInt"] != null && Request["codAplic"] != null)
                {
                    cod_int = Server.UrlDecode(Criptografia.Descriptografar(Request["codInt"]));
                    cod_aplic = Server.UrlDecode(Criptografia.Descriptografar(Request["codAplic"]));
                }
                

                if (!Page.IsPostBack)
                {
                    switch (operacao[0])
                    {

                        case 'D':
                            lblInter.Text = "Excluir Interface";
                            CarregarDados();
                            break;

                        case 'I':
                            lblInter.Text = "Inserir Interface";
                            break;

                        default:
                            break;
                    }
                }
            }

        }

        private void CarregarDados()
        {
            try
            {
                Interface objInter = Interface.Pesquisar(usuAut.Login, cod_int, cod_aplic);
                txtInterface.Text = objInter.Dsc_aplic;
                ddlAplicações.SelectedValue = objInter.Cod_aplic;
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }
            
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Interfaces.aspx");
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                Interface inter;

                switch (operacao[0])
                {                   

                    case 'D':

                        inter = new Interface();
                        inter.Cod_aplic = cod_aplic;
                        inter.Cod_int = cod_int;
                        inter.Dsc_aplic = txtInterface.Text;
                        inter.Excluir(usuAut.Login);

                        break;

                    case 'I':

                        inter = new Interface();
                        inter.Cod_aplic = ddlAplicações.SelectedValue;
                        inter.Cod_int = String.Empty;
                        inter.Dsc_aplic = txtInterface.Text;
                        inter.Inserir(usuAut.Login);

                        break;

                    default:
                        break;
                }

                Response.Redirect("Interfaces.aspx");


            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }

        }
    }
}
